﻿using System;
using System.Data;
using Microsoft.SharePoint;

namespace SKN
{
    public static class DiscussionBoard
    {
        public static DataTable dt = null;

        /// <summary>
        /// This method will retrun all discussions matching filterCriteria
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="filterCriteria"></param>
        /// <param name="renderResponsesAlso"></param>
        public static void RenderMultipleDiscussions (SPList mainList, SPQuery filterCriteria, bool renderResponsesAlso)
        {
            //RenderDiscussion(mainList,folder.ID)

            throw new NotImplementedException();
        }

        /// <summary>
        /// This method will retrun discussions which has been replied recently
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="filterCriteria"></param>
        /// <param name="renderResponsesAlso"></param>
        public static void RenderActiveDiscussions (SPList mainList, SPQuery filterCriteria, bool renderResponsesAlso)
        {

            //RenderDiscussion(mainList,folder.ID)

            throw new NotImplementedException();
        }

        /// <summary>
        /// This method will simple render a Discussion and optionally its responses
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="folder"></param>
        /// <param name="renderResponsesAlso"></param>
        public static void RenderDiscussion (SPList mainList, SPListItem folder, bool renderResponsesAlso,bool renderThreaded)
        {
            Console.WriteLine("Folder Name: " + folder.Name);
            Console.WriteLine("Folder ID: " + folder.ID);

            // Read body of attachment
            Console.WriteLine("Body: " + folder.Fields["Body"].GetFieldValueAsText(folder["Body"]));

            if (renderResponsesAlso)
            {
                if (renderThreaded)
                {
                    RenderAllReponsesForDiscussionThreaded(mainList, folder.ID);
                }
                else
                {
                    RenderAllReponsesForDiscussionFlat(mainList, folder.ID);
                }
            }
        }

        /// <summary>
        /// This method will simple render responses for a dicussion in threaded view mode
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="parentID"></param>
        public static void RenderAllReponsesForDiscussionThreaded (SPList mainList, int parentID)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format("<Where><Eq><FieldRef Name='ParentFolderId' /><Value Type='Number'>{0}</Value></Eq></Where>", parentID.ToString());
            query.ViewFields = @"<FieldRef Name='ParentFolderId' /><FieldRef Name='Threading' /><FieldRef Name='Title' /><FieldRef Name='ID' /><FieldRef Name='Body' />";
            query.ViewAttributes = "Scope='RecursiveAll'";
            dt = mainList.GetItems(query).GetDataTable();

            foreach (DataRow listItem in dt.Rows)
            {
                if (listItem["Threading"].ToString().Length == 56)
                {
                    RenderResponseThreaded(listItem, 56);
                }
            }
        }      

        /// <summary>
        /// This method will render a response in threaded mode
        /// </summary>
        /// <param name="listItemResponse"></param>
        /// <param name="length"></param>
        public static void RenderResponseThreaded (DataRow listItemResponse, int length)
        {
            DataRow[] listItems = dt.Select(string.Format("Threading Like '{0}*' AND LEN(Threading)={1}", listItemResponse["Threading"], length));

            foreach (DataRow item in listItems)
            {
                Console.WriteLine("Item DisplayName: " + item["Title"]); // Returns Title of Discussion 
                Console.WriteLine("List ID: " + item["ID"]);
                //Console.WriteLine("List Folder ID: " + listItem["ParentFolderId"] ); // Returns ID of Parent Discussion 
                Console.WriteLine("Body: " + item["Body"]);

                Console.WriteLine("Threading: " + item["Threading"].ToString().Length);
                int newLength = length + 10;
                RenderResponseThreaded(item, newLength);
            }
        }

        /// <summary>
        /// This method will simple render responses for a dicussion in flat view mode
        /// </summary>
        /// <param name="mainList"></param>
        /// <param name="parentID"></param>
        public static void RenderAllReponsesForDiscussionFlat (SPList mainList, int parentID)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format("<Where><Eq><FieldRef Name='ParentFolderId' /><Value Type='Number'>{0}</Value></Eq></Where>", parentID.ToString());
            query.ViewFields = @"<FieldRef Name='ParentFolderId' /><FieldRef Name='Threading' /><FieldRef Name='Title' /><FieldRef Name='ID' /><FieldRef Name='Body' />";
            query.ViewAttributes = "Scope='RecursiveAll'";
            dt = mainList.GetItems(query).GetDataTable();

            foreach (DataRow listItem in dt.Rows)
            {
                RenderResponseFlat(listItem);
            }
        }
        
        /// <summary>
        /// This method will render a response in flat mode
        /// </summary>
        /// <param name="listItemResponse"></param>
        /// <param name="length"></param>
        public static void RenderResponseFlat (DataRow item)
        {
            Console.WriteLine("Item DisplayName: " + item["Title"]); // Returns Title of Discussion 
            Console.WriteLine("List ID: " + item["ID"]);
            //Console.WriteLine("List Folder ID: " + listItem["ParentFolderId"] ); // Returns ID of Parent Discussion 
            Console.WriteLine("Body: " + item["Body"]);

            Console.WriteLine("Threading: " + item["Threading"].ToString().Length);
        }
    }
}
